package com.sm.healthy.utils;

import android.annotation.SuppressLint;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

/**
 * 时间工具
 * Created by HDL on 2017/7/25.
 */

public class DateUtils {
    @SuppressLint("SimpleDateFormat")
    private static SimpleDateFormat format = new SimpleDateFormat("HH:mm");

    /**
     * 获取当前时间的起点（00:00:00）
     *
     * @param currentTime
     * @return
     */
    public static long getTodayStart(long currentTime) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date(currentTime));
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTimeInMillis();
    }

    /**
     * 获取当前时间的小时值
     *
     * @param currentTime
     * @return
     */
    public static int getHour(long currentTime) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date(currentTime));
        return calendar.get(Calendar.HOUR_OF_DAY);
    }

    /**
     * 获取当前时间的分钟值
     *
     * @param currentTime
     * @return
     */
    public static int getMinute(long currentTime) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date(currentTime));
        return calendar.get(Calendar.MINUTE);
    }

    /**
     * 获取当前时间的分钟值
     *
     * @param currentTime
     * @return
     */
    public static int getSecond(long currentTime) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date(currentTime));
        return calendar.get(Calendar.SECOND);
    }

    /**
     * 获取当前时间的分钟值+毫秒值
     *
     * @param currentTime
     * @return
     */
    public static int getMinuteMillisecond(long currentTime) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date(currentTime));
        return calendar.get(Calendar.MINUTE) * 60 * 1000 + calendar.get(Calendar.SECOND) * 1000 + calendar.get(Calendar.MILLISECOND);
    }

    /**
     * 获取当前时间的毫秒值
     *
     * @param currentTime
     * @return
     */
    public static int getMillisecond(long currentTime) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date(currentTime));
        return calendar.get(Calendar.MILLISECOND);
    }

    /**
     * 获取当前时间的终点（23:59:59）
     *
     * @param currentTime
     * @return
     */
    public static long getTodayEnd(long currentTime) {
        return getTodayStart(currentTime) + 24 * 60 * 60 * 1000L - 1000;
    }

    /**
     * 获取指定时间的年月日
     *
     * @param currentTime
     * @return
     */
    @SuppressLint("SimpleDateFormat")
    public static String getDateByCurrentTiem(long currentTime) {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(currentTime);
    }

    /**
     * 获取指定时间的年月日
     *
     * @param currentTime
     * @return
     */
    @SuppressLint("SimpleDateFormat")
    public static String getDateTime(long currentTime) {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(currentTime);
    }

    /**
     * 日期转星期
     *
     * @param datetime
     * @return
     */
    public static String dateToWeek(String datetime) {
        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
        String[] weekDays = {"Sun", "Mon", "Tue", "Wed", "Thur", "Fri", "Sat"};
        Calendar cal = Calendar.getInstance(); // 获得一个日历
        Date datet = null;
        try {
            datet = f.parse(datetime);
            if (datet != null)
                cal.setTime(datet);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1; // 指示一个星期中的某天。
        if (w < 0)
            w = 0;
        return weekDays[w];
    }


    /**
     * 获取指定时间的年月日
     *
     * @param currentTime
     * @return
     */
    @SuppressLint("SimpleDateFormat")
    public static String getDateTimeMinute(long currentTime) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        TimeZone aDefault = TimeZone.getDefault();
        String id = aDefault.getID();
        simpleDateFormat.setTimeZone(TimeZone.getTimeZone(id));

        return simpleDateFormat.format(currentTime);
    }

    /**
     * 获取时区
     *
     * @return
     */
    @SuppressLint("SimpleDateFormat")
    public static int getTimeZoneNum() {
        TimeZone aDefault = TimeZone.getDefault();
        TimeZone timeZone = TimeZone.getTimeZone(aDefault.getID());
        int rawOffset = timeZone.getRawOffset() / 3600000;
        return rawOffset;
    }

    /**
     * 获取指定时间的年月日
     *
     * @param currentTime
     * @return
     */
    @SuppressLint("SimpleDateFormat")
    public static String getDateTimeDay(long currentTime) {
        return new SimpleDateFormat("yyyy-MM-dd").format(currentTime);
    }

    /**
     * 获取指定时间的年月日
     *
     * @param currentTime
     * @return
     */
    @SuppressLint("SimpleDateFormat")
    public static String getDateTimeDay2(long currentTime) {
        return new SimpleDateFormat("yyyy-M-dd").format(currentTime);
    }

    /**
     * 获取指定时间的年月日
     *
     * @param currentTime
     * @return
     */
    @SuppressLint("SimpleDateFormat")
    public static String getDateTimeDayToMonth(long currentTime) {
        return new SimpleDateFormat("MM/dd").format(currentTime);
    }

    /**
     * 获取指定英文时间的年月日时分秒
     *
     * @param currentTime
     * @return
     */
    @SuppressLint("SimpleDateFormat")
    public static String getDateTimeByPMForAM(long currentTime) {
        return new SimpleDateFormat("MMM dd yyy hh:mm:ss aa", Locale.ENGLISH).format(currentTime);
    }

    /**
     * 获取指定英文时间的时分秒
     *
     * @param currentTime
     * @return
     */
    @SuppressLint("SimpleDateFormat")
    public static String getDateTimeByPMForLand(long currentTime) {
        return new SimpleDateFormat("h:mm aa", Locale.ENGLISH).format(currentTime);
    }

    /**
     * 获取指定英文时间的年月日
     *
     * @param currentTime
     * @return
     */
    @SuppressLint("SimpleDateFormat")
    public static String getDateTimeByEn(long currentTime) {
        return new SimpleDateFormat("MMM dd yyy HH:mm:ss", Locale.ENGLISH).format(currentTime);
    }

    /**
     * 获取指定英文时间的年月日
     *
     * @param currentTime
     * @return
     */
    @SuppressLint("SimpleDateFormat")
    public static String getDateTimeByEnHour(long currentTime) {
        return new SimpleDateFormat("MMM dd yyy HH:mm", Locale.ENGLISH).format(currentTime);
    }

    /**
     * 获取指定英文时间的年月日
     *
     * @param currentTime
     * @return
     */
    @SuppressLint("SimpleDateFormat")
    public static String getDateTimeByEnDay(long currentTime) {
        return new SimpleDateFormat("MMM dd yyy", Locale.ENGLISH).format(currentTime);
    }

    /**
     * 获取时分秒
     *
     * @param currentTime
     * @return
     */
    @SuppressLint("SimpleDateFormat")
    public static String getTimeHour(long currentTime) {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.sss").format(currentTime);
    }

    /**
     * 获取时分秒
     *
     * @param currentTime
     * @return
     */
    @SuppressLint("SimpleDateFormat")
    public static String getTimeMinute(long currentTime) {
        return new SimpleDateFormat("MM-dd HH:mm").format(currentTime);
    }

    /**
     * 获取指定英文时间的月日
     *
     * @param currentTime
     * @return
     */
    @SuppressLint("SimpleDateFormat")
    public static String getDateTimeMonthDay(long currentTime) {
        return new SimpleDateFormat("MMM dd", Locale.ENGLISH).format(currentTime);
    }

    /**
     * 获取指定时间的年月日
     *
     * @param currentTime
     * @return
     */
    @SuppressLint("SimpleDateFormat")
    public static String getDateMonthDay(long currentTime) {
        return new SimpleDateFormat("MM-dd").format(currentTime);
    }

    /**
     * 根据下标获取HH:mm格式的时间
     *
     * @param timeIndex
     * @return
     */
    public static String getHourMinute(int timeIndex) {
        return format.format(timeIndex * 60 * 1000 + 16 * 60 * 60 * 1000);
    }

    /**
     * 获取指定日期的时间（如：10:11:12）
     *
     * @param currentTime
     * @return
     */
    public static String getTime(long currentTime) {
        @SuppressLint("SimpleDateFormat") SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
        return format.format(new Date(currentTime));
    }

    /**
     * 获取指定日期的时间（如：11:12）
     *
     * @param currentTime
     * @return
     */
    public static String getTime2(long currentTime) {
        @SuppressLint("SimpleDateFormat") SimpleDateFormat format = new SimpleDateFormat("mm:ss");
        return format.format(new Date(currentTime));
    }

    /**
     * 根据当前的秒数计算时间
     *
     * @param currentSecond
     * @return
     */
    public static String getTimeByCurrentSecond(int currentSecond) {
        currentSecond = currentSecond / 60;
        int minute = currentSecond % 60;
        int hour = currentSecond / 60;
        if (hour >= 24) {
            hour = hour % 24;
        }
        return (hour < 10 ? "0" + hour : hour) + ":" + (minute < 10 ? "0" + minute : minute);
    }

    /**
     * 根据当前的秒数计算时间
     *
     * @param currentSecond
     * @return
     */
    public static String getTimeByCurrentHours(int currentSecond) {
        currentSecond = currentSecond * 10;
        currentSecond = currentSecond / 60;
        int minute = currentSecond % 60;
        int hour = currentSecond / 60;
        if (hour >= 24) {
            hour = hour % 24;
        }
        return (hour < 10 ? "0" + hour : hour) + ":" + (minute < 10 ? "0" + minute : minute);
    }

    //    public static void main(String[] args) {
    //        TimeSlot timeSlot = new TimeSlot(DateUtils.getTodayStart(System.currentTimeMillis()), DateUtils.getTodayStart(System.currentTimeMillis()) - 60 * 60 * 1000, DateUtils.getTodayStart(System.currentTimeMillis()) + 120 * 60 * 1000);
    //        System.out.println(timeSlot.toString());
    //    }

    /**
     * 判断时间是否在时间段内
     *
     * @param nowTime
     * @param beginTime
     * @param endTime
     * @return
     */
    public static boolean isCurrentTimeArea(long nowTime, long beginTime, long endTime) {
        return nowTime >= beginTime && nowTime <= endTime;
    }

    /**
     * 判断指定时间段是否包含在某个时间段
     *
     * @return
     */
    public static boolean isContainTime(TimeSlot timeSlot, long beginTime, long endTime) {
        //        ELog.e(timeSlot);
        //        ELog.e("beginTime = "+beginTime);
        //        ELog.e("endTime = "+endTime);
        return beginTime >= timeSlot.getStartTimeMillis() && endTime <= timeSlot.getEndTimeMillis();
    }

    //移动到某时间  传入参数格式举例 20170918120000
    @SuppressLint("SimpleDateFormat")
    public static long strTimeToLong(String timeStr) {
        try {
            Date parse = new SimpleDateFormat("yyyy-MM-dd").parse(timeStr);
            assert parse != null;
            return parse.getTime();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }
}
